package com.qnkf.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author colne
 */
@Component
public class WebApiFilter extends ZuulFilter {
    private static final Logger log = LoggerFactory.getLogger(WebApiFilter.class);

    @Override
    public String filterType() {
        // 前置过滤器
        return "pre";
    }

    @Override
    public int filterOrder() {
        // 优先级为0，数字越大，优先级越低
        return 0;
    }

    /**
     * 是否执行过滤
     */
    @Override
    public boolean shouldFilter() {
//        RequestContext ctx = RequestContext.getCurrentContext();
//        HttpServletRequest request = ctx.getRequest();
//        String url = request.getRequestURL().toString();
//        if (null != url && url.contains("PC")){
//            return false;
//        }
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        log.info("进入过滤器");
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        String token = request.getHeader("token");
        String url = request.getRequestURL().toString();
        log.info("url:" + url);
        log.info("token:" + token);
//        if(StringUtils.isEmpty(request.getHeader("company_key"))){
//            log.info("错误请求");
//            // 过滤该请求，不对其进行路由
//            ctx.setSendZuulResponse(true);
//            return "Wrong request";
//        }
        return null;
    }
}
